<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Search_private extends wsFrontend
{

    public function __construct()
    {
        parent::__construct();
        if (strpos(strtolower($this->uri->uri_string()), 'search_private') !== false) {
            redirect();
        }
        $this->db->cache_set_path(dirname($this->db->cachedir) . '/Search_page/');
    }

    public function index()
    {
        $this->tpl->hide_search = true;
        $data['url'] = $this->route_links->get('search_page');
        $this->load->model(array('wallpaper_model', 'category_model'));

        $filter['show_disabled'] = FALSE;
        $filter['per_page'] = $config['per_page'] = $this->config->item('items_per_page');
        $page_number = (int)$this->input->get('page');
        $filter['offset'] = ($page_number ? ($page_number - 1) * $config['per_page'] : 0);
        $filter['bypath'] = false;

        $data['keyword'] = trim($this->input->get('q'));

        if ($data['keyword'])
            $filter['name'] = $data['keyword'];
        
        $this->db->custom_seqgment('search', 'index');
        $data['categories'] = $this->category_model->get_categoryDropDownTree();
        $this->db->reset_custom_seqgment();


        if ($this->input->get('category') && array_key_exists($this->input->get('category'), $data['categories']))
            $filter['category'] = $this->input->get('category');

        if (!empty($_GET['sort']))
            $filter['sort'] = $_GET['sort'];

        if (isset($_GET['time']))
            $filter['time'] = $_GET['time'];

        if (isset($_GET['color'])) {
            $color = $_GET['color'];
            $this->load->library('colors_tool');
            if (ctype_xdigit($color) && strlen($color) == 6) {
                $color_tool = new colors_tool($color);
                $filter['colors'] = $color_tool->similar_colors_by_hex(40, 20);
                if (empty($_GET['sort'])) {
                    $_GET['sort'] = $filter['sort'] = 'colors';
                }
            }
        }
		$data['colors'] = trim($this->input->get('colors'));

        $data['total_count'] = $this->wallpaper_model->getWallpapers($filter + array('count' => true));

        //Initialize Pagination
        $config['base_url'] = $data['url'];
        $config['total_rows'] = $data['total_count'];
        $config['custom_query_string'] = TRUE;
		$config['use_page_numbers'] = TRUE;
		$config['page_query_string'] = TRUE;
		$config['enable_query_strings'] = TRUE;
        $config['query_string_segment'] = 'page';

        if (!empty($_GET)) {
		    $get_method = '?' . http_build_query($this->input->get());
            $config['prefix'] = ($get_method ? $get_method . '&' : '?') . 'page=';
            $config['first_url'] = $config['base_url'] . $get_method;
        } else {
            $config['prefix'] = '?page=';
        }

        $this->load->library('pagination');
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $data['wallpapers'] = $this->wallpaper_model->getWallpapers($filter);


        $this->tpl->breadcrumbs['壁纸搜索'] = $data['url'];
        $this->tpl->breadcrumbs[' 搜索到 ' . ($data['keyword'] ? $data['keyword'] : '相关').' 约有'.$data['total_count'].'张壁纸'] = 'active';

        $SearchReplace = array(
            '{keyword}' => ($data['keyword'] ? $data['keyword'] : '相关')
        );

        $this->tpl->meta ['title'] = $this->metadata->get_metadata('_search_index_title', 'search_page', $SearchReplace, false, $page_number);
        $this->tpl->meta ['description'] = $this->metadata->get_metadata('_search_index_description', 'search_page', $SearchReplace, false, $page_number);
        $this->tpl->meta ['keywords'] = $this->metadata->get_metadata('_search_index_keywords', 'search_page', $SearchReplace, false, $page_number);


        $this->tpl->contents [] = $this->tpl->fetch('contents/search_index', $data);
        $this->tpl->render('layouts/empty');
    }

}